<?php

namespace app\admin\controller;

use app\common\builder\ZBuilder;
use app\common\model\Company as CompanyModel;
use think\facade\Session;


class Company extends Admin
{
    public function index()
    {
        $map = $this->getMap();
       if (UID > 1) $map =['id'=>COMID];
        $order = $this->getOrder('create_time desc');
        $list = CompanyModel::where($map)->order($order)->paginate();
        $page = $list->render();
        return ZBuilder::make('table')
            ->addColumns([
                ['id', 'ID'],
                ['company_name', '企业名称', 'text.edit'],
                ['app_key', '企业ID', 'text'],
                ['create_time', '创建时间', 'datetime'],
                ['status', '状态','switch'],
                ['right_button', '操作', 'btn']
            ])
            //批量添加顶部按钮
            ->addTopButtons('add,enable,disable')
            // 批量添加右侧按钮
            ->addRightButtons('edit')
            //设置排序
            ->addOrder('id,create_time')
            // 设置搜索参数
            ->setSearch(['company_name' => '名称','id'=>'ID号'])
            //设置筛选
            ->addFilterList('status', [0=>'禁用', 1=>'启用'])
            ->setTableName('admin_company')
            ->setRowList($list)
            ->setPages($page)
            ->fetch();
    }

    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Company');
            if(true !== $result) $this->error($result);
            $data['app_secret'] = password_hash(time()*rand(1,99),1);
            if ($re=CompanyModel::create($data)) {
                $re->app_key = md5(think_encrypt($re['id'].rand(1,99)));//加密
                $re->save();
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','uid',UID],
                ['text', 'company_name', '企业名称'],
                //['password', 'app_secret', 'secret密钥'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->layout(['company_name'=>6])
            ->fetch();
    }

    //编辑
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Company');
            if(true !== $result) $this->error($result);
            if (CompanyModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        $info = CompanyModel::get($id);
        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['text', 'company_name', '企业名称'],
                ['static', 'app_key', '企业ID'],
                ['static','app_secret', '密钥'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->setFormdata($info)
            ->layout(['company_name'=>6,'app_key'=>6])
            ->fetch();
    }
}